package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.account.data.AccountBean;
import com.sskj.mine.asset.data.AddressBean;
import com.sskj.mine.asset.data.AddressListBean;
import com.sskj.mine.asset.data.RecordsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/05/04
 */
public class AddressListActivity extends BaseActivity<AddressListPresenter> {


    @BindView(R2.id.add_coin_img)
    ImageView addCoinImg;
    @BindView(R2.id.coin_list)
    RecyclerView coinList;
    @BindView(R2.id.add_usdt)
    ImageView addUsdt;
    @BindView(R2.id.usdt_list)
    RecyclerView usdtList;

    private SlimAdapter usdtAdapter;
    private SlimAdapter coinAdapter;
    private List<AddressBean> data;


    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_address_list;
    }

    @Override
    public AddressListPresenter getPresenter() {
        return new AddressListPresenter();
    }

    @Override
    public void initView() {
        setTitle(getString(R.string.mine_addressListActivity1));
        usdtList.setLayoutManager(new LinearLayoutManager(this));
        coinList.setLayoutManager(new LinearLayoutManager(this));
        usdtAdapter = SlimAdapter.create().register(R.layout.mine_item_address, new SlimInjector<AddressBean>() {
            @Override
            public void onInject(AddressBean data, IViewInjector injector, List<Object> list) {
                injector
                        .text(R.id.name, data.getNotes())
                        .text(R.id.address_url, data.getAddress())
                        .clicked(R.id.mineTextview6, v -> {
                            TipUtil.getSureCancelTip(AddressListActivity.this, getString(R.string.mine_addressListActivity2), () -> {
                                mPresenter.delete(data.getType(), data.getAddress(), data.getId());
                            });
                        });
            }

        }).attachTo(usdtList);
        coinAdapter = SlimAdapter.create().register(R.layout.mine_item_address, new SlimInjector<AddressBean>() {
            @Override
            public void onInject(AddressBean data, IViewInjector injector, List<Object> list) {
                injector
                        .text(R.id.name, data.getNotes())
                        .text(R.id.address_url, data.getAddress())
                        .clicked(R.id.mineTextview6, v -> {
                            TipUtil.getSureCancelTip(AddressListActivity.this, getString(R.string.mine_addressListActivity2), () -> {
                                mPresenter.delete(data.getType(), data.getAddress(), data.getId());
                            });
                        });
            }

        }).attachTo(coinList);
        ItemClickSupport.addTo(usdtList)
                .setOnItemClickListener((recyclerView, i, view) -> {
                    RxBus.getDefault().post(usdtAdapter.getDataItem(i));
                    finish();
                });
        ItemClickSupport.addTo(coinList)
                .setOnItemClickListener((recyclerView, i, view) -> {
                    RxBus.getDefault().post(usdtAdapter.getDataItem(i));
                    finish();
                });
    }

    @Override
    public void initData() {
        mPresenter.addressList();
    }

    @Override
    protected void initEvent() {
        ClickUtil.click(addCoinImg, () -> AddAddressActivity.start(this, "2"));
        ClickUtil.click(addUsdt, () -> AddAddressActivity.start(this, "1"));
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AddressListActivity.class);
        context.startActivity(intent);
    }

    public void updateUI(AddressListBean data) {
        usdtAdapter.updateData(data.getBtc());
        coinAdapter.updateData(data.getEth());
    }

    @Override
    protected void onResume() {
        if (mPresenter != null) {
            mPresenter.addressList();
        }
        super.onResume();
    }

    public void deleteSuccess() {
        mPresenter.addressList();
    }
}
